﻿using System.Collections.Generic;
using MediaPortal.Configuration;
using MediaPortal.Dialogs;
using MediaPortal.GUI.Library;
using MediaPortal.Profile;
using FlowDirection = System.Windows.FlowDirection;

namespace GeckoBrowser.Utils
{
    public static class GeckoBrowserUtils
    {
        private static readonly Logger logger = Logger.GetInstance();

        public static bool ShowKeyboard(ref string sString, bool WebKB)
        {
           
            //Application.DoEvents();
            //WebBrowserManager.SuspendBrowser();
            //InitializeControlsDelegate d = new InitializeControlsDelegate(ShowKeyboard);
            //GUIGraphicsContext.form.Invoke(d);
            logger.WriteLog("Show virtual keyboard", LogLevel.Info);
            VirtualKeyboard keyboard = (VirtualKeyboard)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_VIRTUAL_KEYBOARD);
            if (null == keyboard)
            {
                return false;
            }
            keyboard.IsSearchKeyboard = false;
            keyboard.Reset();
            keyboard.Text = sString;
            keyboard.IsOverlayAllowed = true;
            //keyboard.FullScreenVideoAllowed = true;
            keyboard.Focus();
            keyboard.DoModal(GUIWindowManager.ActiveWindow);

            if (keyboard.IsConfirmed)
            {
                sString = keyboard.Text;
                //WebBrowserManager.ResumeBrowser();
            }
            
            return keyboard.IsConfirmed;

        }
        public static void ShowDialogOkWindow(string header, string text)
        {
            logger.WriteLog("Show dialog window", LogLevel.Info);
            GUIDialogOK window = (GUIDialogOK)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_OK);
            window.SetHeading(header);
            List<string> textCol = new List<string>();
            int lastIndex = 0;
            for (int i = 44; i < text.Length; i += 45)
            {
                if (text[i] != ' ' || text[i] != ',' || text[i] != '.')
                {
                    while (i > 1)
                    {
                        i--;
                        if (text[i] == ' ' || text[i] == ',' || text[i] == '.')
                        {
                            break;
                        }
                    }
                }
                textCol.Add(text.Substring(lastIndex, i - lastIndex));
                lastIndex = i;
            }
            textCol.Add(text.Substring(lastIndex, text.Length - lastIndex));
            for (int i = 0; i < textCol.Count; i++) textCol[i] = textCol[i].Trim();
            for (int i = 1; i <= 4; i++)
            {
                if (i <= textCol.Count) window.SetLine(i, textCol[i - 1]);
            }
            window.FlowDirection = FlowDirection.RightToLeft;
            window.DoModal(GUIWindowManager.ActiveWindow);

        }
        public static void SetProperty(string property, string value)
        {
            GUIPropertyManager.SetProperty(property, value);
            if (GUIPropertyManager.Changed) logger.WriteLog("Set property \"" + property + "\" to \"" + value + "\" successful", LogLevel.Debug);
            else logger.WriteLog("Set property \"" + property + "\" to \"" + value + "\" failed", LogLevel.Warning);
        }
        public static void InitLog()
        {
            using (Settings settings = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml")))
            {
                Logger.LogFile = Config.GetFile(Config.Dir.Log, "geckobrowser.log");
                Logger.LogDebug = settings.GetValueAsBool("GeckoBrowser", "LogDebug", false);
                Logger.LogWarning = settings.GetValueAsBool("GeckoBrowser", "LogWarning", true);
                Logger.LogError = settings.GetValueAsBool("GeckoBrowser", "LogError", true);
            }
        }
        

    }
}
